<template>
  <div class="replace-box">
    <el-dialog width="600px" top="5%" title="线下代充" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="toShopId" label="代充商户">
                <el-select style="width:90%" :disabled="disab" filterable v-model="dialogForm.toShopId" @change="shopChange"
                  placeholder="请选择代充商户">
                  <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="leftFee" label="账户余额(元)">
                <span>{{ numFormat(dialogForm.leftFee) || '' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="receiveAccount" label="收款账户">
                <el-select style="width:90%" filterable v-model="dialogForm.receiveAccount" placeholder="请选择收款账户">
                  <el-option v-for="item in accountOptions" :key="item.receiveAccount" :label="item.showName"
                    :value="item.receiveAccount" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="fee" label="收款金额">
                <el-input style="width:90%;" v-model="dialogForm.fee" type="number" min="0" placeholder="请输入收款金额">
                  <template #append>元</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="payUserName" label="打款人">
                <el-input style="width:90%;" v-model="dialogForm.payUserName" placeholder="请输入打款人" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="chargeTime" label="支付时间">
                <el-date-picker style="width:90% !important;" v-model="dialogForm.chargeTime" type="datetime" value-format="x"
                  placeholder="请选择支付时间" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="payType" label="支付方式">
                <el-input style="width:90%;" v-model="dialogForm.payType" placeholder="请输入支付方式" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="tradeNo" label="支付流水号">
                <el-input style="width:90%;" v-model="dialogForm.tradeNo" placeholder="请输入支付流水号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="remark" label="备注">
                <el-input v-model="dialogForm.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="password" label="登录密码">
                <el-input style="width:90%" v-model="dialogForm.password" type="password" show-password placeholder="请输入登录密码" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import { numFormat, successDeal, warningDeal, accDiv } from '@/utils/utils'
import api from '../api'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      disab: false,
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        toShopId: [
          { required: true, message: '请选择代充商户', trigger: 'blur' },
        ],
        chargeTime: [
          { required: true, message: '请选择支付时间', trigger: 'blur' },
        ],
        fee: [
          { required: true, message: '请输入支付金额', trigger: 'blur' },
          // {
          //   pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于等于0的数字', trigger: 'blur'
          // }, 
        ],
        payType: [
          { required: true, message: '请输入支付方式', trigger: 'blur' },
        ],
        payUserName: [
          { required: true, message: '请输入打款人', trigger: 'blur' },
        ],
        receiveAccount: [
          { required: true, message: '请选择收款账户', trigger: 'blur' },
        ],
        tradeNo: [
          { required: true, message: '请输入支付流水号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ],
      },
      shopOptions: [],
      accountOptions: [],
    })

    function dialogShow(type, data) {
      if (type == 2) {
        state.disab = true
        state.dialogForm.toShopId = data.shopId
        state.dialogForm.leftFee = data.leftFee
      }
      getShopOptions()
      getAccountOptions()
      state.dialogFormVisible = true
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          //校验通过
          const obj = {
            toShopId: state.dialogForm.toShopId || null,
            chargeTime: state.dialogForm.chargeTime || null,
            fee: (state.dialogForm.fee * 100) || null,
            payType: state.dialogForm.payType || null,
            tradeNo: state.dialogForm.tradeNo || null,
            remark: state.dialogForm.remark || null,
            payUserName: state.dialogForm.payUserName || null,
            receiveAccount: state.dialogForm.receiveAccount || null,
            password: hexMD5(state.dialogForm.password) || null,
          }
          api.accountOfflineSubmit(obj).then(res => {
            if (res.code == '200') {
              successDeal(res.msg)
              context.emit('dialogClose')
              state.dialogFormVisible = false
            } else {
              warningDeal(res.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      })
    }

    function getShopOptions() {
      api.shopListAll().then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function shopChange(e) {
      state.dialogForm.leftFee = null
      getShopDetail(e)
    }

    function getShopDetail(shopId) {
      const obj = {
        shopId
      }
      api.shopDetail(obj).then(res => {
        if (res.code == '200') {
          state.dialogForm.leftFee = accDiv(res.data.leftFee, 10000)
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getAccountOptions() {
      api.accountList().then(res => {
        if (res.code == '200') {
          state.accountOptions = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      state.disab = false
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      submitHandle,
      dialogShow,
      numFormat,
      shopChange,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-divider--horizontal) {
  margin: 5px 0 !important;
}
:deep(.el-input-group__append) {
  padding: 0 20px !important;
}
</style>